<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack()">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            class="eidt-el-form"
            @change="getType"
          >
            <el-form-item label="名称" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <!--<el-form-item label="类型" prop="type">
              <el-select v-model="ruleForm.type" placeholder="请选择">
                <el-option v-for="(t,i) in types" :key="i" :label="t.name" :value="t.value"  ></el-option>
              </el-select>
            </el-form-item>-->
            <el-form-item label="注册通道" prop="registerId">
              <el-select v-model="ruleForm.registerId" placeholder="请选择">
                <el-option v-for="(t,i) in registers" :key="i" :label="t.name" :value="t.registerId"  ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>

var types=[
    {name:"组委会",value:"1"},
    {name:"展商",value:"2"},
    {name:"展团/代表团",value:"3"},
    {name:"观众" ,value:"4"},
    {name:"媒体", value:"5"},
    {name:"嘉宾" ,value:"6"},
    {name:"搭建商" ,value:"7"},
    {name:"服务商" ,value:"8"},
    {name:"撤展人员" ,value:"9"},
    {name:"兼职/志愿者/外协" ,value:"10"},
    {name:"会议/论坛/活动" ,value:"11"},
    {name:"其他" ,value:"12"}
  ];

export default {
  name: "editSource",
  data() {
    return {
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        registerId: [{ required: true, message: "请选择注册通道", trigger: "blur" }],
        //type: [{ required: true, message: "请选择类型", trigger: "blur" }]
      },
      types:types,
      registers:[],
      ruleForm: {
        name: "",
        registerId: "",
        channel: "",
        type: "",
        projectId: localStorage.getItem("project_Id")
      },
      sourceId: ""
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object
    }
  },
  created() { 
      
this.$axios
    .get("/Register/Project/" + this.ruleForm.projectId)
    .then(res => {
        if (res.data.hasError == false) {
            this.registers=[...res.data.data];
        }
    });
  },
  watch: {
    editData: function() {
      const item = this.editData;
      this.ruleForm = item;
      this.sourceId=item.sourceId;
    }
  },
  methods: {
    saveClick(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (this.sourceId == "" || this.sourceId == undefined) {
            this.$axios.post("/Source/", this.ruleForm).then(res => {
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
              } else {
                this.$message.error("保存失败！");
              }
            });
          } else {
            this.$axios
              .put("/Source/" + this.sourceId + "", this.ruleForm)
              .then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                } else {
                  this.$message.error("编辑失败！");
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$refs.ruleForm.resetFields();
      this.$emit("backFunStu");
      this.sourceId = "";
    },
    getType(value) {
      this.ruleForm.type = value;
    }
  }
};
</script>